package com.jiang.books;
import com.jiang.books.domain.Profile;
import com.jiang.books.domain.User;
import com.jiang.books.domain.UserGroup;
import com.jiang.books.service.*;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

public class TestMain {
    public static void main(String a[]) {
        Locale.setDefault(Locale.ENGLISH);
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
        ctx.load("classpath:spring-app-context.xml");
        System.out.println("Loaded Long.MAX_VALUE"+Long.MAX_VALUE);
        System.out.println("l1=l2"+(new Long(1) == new Long(1)));
        ctx.refresh();
        //testUserGroup(ctx);
        //testUser(ctx);
        //testUserGroupSpData(ctx);
        //testUserSpData(ctx);
        testProfile(ctx);
        //javax.servlet.ServletException ex;
    }

    private static void testProfile(GenericXmlApplicationContext ctx) {
        IProfileService userGroupService = (IProfileService)ctx.getBean("profileSpDataService");
        Profile profile = userGroupService.findById(32001L);
        System.out.println("testProfile: profile="+profile);
    }

    static void testUserGroup(GenericApplicationContext ctx){
        IUserGroupService userGroupService = (IUserGroupService)ctx.getBean("userGroupServiceJpa");

        UserGroup ug8889 = userGroupService.findById(8889L);
        UserGroup newUG = new UserGroup(ug8889, "test1", "test desc");
        newUG.setId(555L);
        userGroupService.save(newUG);
        System.out.println("saved newUG="+newUG);

        userGroupService.delete(newUG);
        System.out.println("saved and deleted newUG="+newUG);

        List<UserGroup> c = userGroupService.findAllChilderenByNativeQuery(8889L);
        System.out.println("ug8889="+ug8889);
        for ( UserGroup ug : c){
            System.out.println(ug);
        }
    }

    static void testUser(GenericApplicationContext ctx){
        IUserService userService = (IUserService)ctx.getBean("userServiceJpa");
        IUserGroupService userGroupService = (IUserGroupService)ctx.getBean("userGroupServiceJpa");

        //User ug8889 = userService.findById(9007L);

        User newuser = new User("myLogin", "myPass", "Pavlo Poliakov", userGroupService.findById(8889L));
        newuser.setId(555L);
        userService.save(newuser);
        System.out.println("saved newUG="+newuser);
        //userService.delete(newuser);
        //System.out.println("saved and deleted newUG="+newuser);

    }

    static void testUserGroupSpData(GenericApplicationContext ctx){
        System.out.println(" testUserGroupSpData -----------------\n");
        IUserGroupService userGroupService = (IUserGroupService)ctx.getBean("userGroupSpDataService");

        UserGroup newUG = new UserGroup(userGroupService.findById(8889L), "test1", "test desc");
        userGroupService.save(newUG);
        System.out.println("saved newUG="+newUG);
        newUG.setGroupDescription("changed desc");
        userGroupService.save(newUG);
        System.out.println("saved newUG 2="+newUG);

        userGroupService.delete(newUG);
        System.out.println("saved and deleted newUG="+newUG);

        UserGroup newUG2 = new UserGroup(userGroupService.findById(8889L), "test2", "test desc2");
        newUG2.setId(674067064L);
        userGroupService.save(newUG2);
        System.out.println("saved newUG2="+newUG2);
        newUG2.setGroupDescription("BALLLA AHHAAH");
        userGroupService.save(newUG2);
        System.out.println("saved newUG2="+newUG2);
    }

    static void testUserSpData(GenericApplicationContext ctx){
        System.out.println(" GenericApplicationContext -----------------\n");
        IUserService userService = (IUserService)ctx.getBean("userSpDataService");
        IUserGroupService userGroupService = (IUserGroupService)ctx.getBean("userGroupSpDataService");
        for ( User u : userService.findAll()){
            System.out.println(u);
        }

        //User ug8889 = userService.findById(9007001L);

        User newuser = new User("myLogin", "myPass", "Pavlo Poliakov", userGroupService.findById(8889L));
        newuser.setId(555L);
        userService.save(newuser);
        System.out.println("saved newUG="+newuser);
    }
}
